
import 'dart:math';

import 'package:elevator/get/message.dart';
import 'package:elevator/pages/setting/setting_state.dart';
import 'package:elevator/widgets/globel_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:get/get.dart';

import 'setting_logic.dart';

class SettingPage extends StatelessWidget {

  late SettingLogic logic;
  late SettingState state;

  SettingPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    logic = Get.put(SettingLogic());
    state = Get.find<SettingLogic>().state;
    return Scaffold(
        appBar: AppBar(
          leading: IconButton(
            icon: const Icon(
              Icons.navigate_before,
              size: 36,
            ),
            onPressed: () {
              Get.back();
            },
          ),
          centerTitle: true,
          title:
              const Image(image: AssetImage("images/logo.png"), width: 100.0),
        ),
        body: Container(
            width: double.infinity,
            height: double.infinity,
            decoration: const BoxDecoration(
              image: DecorationImage(
                image: AssetImage('images/app_bg.png'),
                fit: BoxFit.cover,
              ),
            ),
            child: Container(
              margin: const EdgeInsets.fromLTRB(10, 10, 10, 40),
              color: Colors.white,
              alignment: Alignment.center,
              child:  SingleChildScrollView(
              scrollDirection: Axis.vertical,
              child:Column(
                          children: [
                            buildButtons(context,logic, state),
                            buildAccountInfoView(context,logic, state)
                          ],
                        ),
              )
            )));
  }

  buildButtons(BuildContext context,SettingLogic logic, SettingState state) {
    return Column(
      children: [
        Container(
            margin: const EdgeInsets.fromLTRB(10,40,10,10),
            width: double.infinity,
            child: ElevatedButton(
              style: ElevatedButton.styleFrom(
                padding: const EdgeInsets.fromLTRB(0, 10, 0, 10),
                foregroundColor: Colors.white,
                backgroundColor: Colors.blue,
                textStyle: const TextStyle(fontSize: 20),
              ),
              onPressed: () => showSelectLanguageDialog(context),
              child: Text('${"setting_language".tr}  中（繁）| Eng'), //选中的颜色字体颜色
            )),
        Container(
            margin: const EdgeInsets.all(10),
            width: double.infinity,
            child: ElevatedButton(
              style: ElevatedButton.styleFrom(
                padding: const EdgeInsets.fromLTRB(0, 10, 0, 10),
                foregroundColor: Colors.white,
                backgroundColor: Colors.blue,
                textStyle: const TextStyle(fontSize: 20),
              ),
              onPressed: () => logic.changeLanguage(),
              child: Obx(()=>Text('${"setting_version".tr}: ${state.version.value}')), //选中的颜色字体颜色
            )),
        Container(
            margin: const EdgeInsets.all(10),
            width: double.infinity,
            child: ElevatedButton(
              style: ElevatedButton.styleFrom(
                padding: const EdgeInsets.fromLTRB(0, 10, 0, 10),
                foregroundColor: Colors.white,
                backgroundColor: Colors.blue,
                textStyle: const TextStyle(fontSize: 20),
              ),
              onPressed: () => logic.clearFileCache(),
              child: Text('updateImg'.tr), //选中的颜色字体颜色
            )),
        Container(
            margin: const EdgeInsets.all(10),
            width: double.infinity,
            child: ElevatedButton(
              style: ElevatedButton.styleFrom(
                padding: const EdgeInsets.fromLTRB(0, 10, 0, 10),
                foregroundColor: Colors.white,
                backgroundColor: Colors.blue,
                textStyle: const TextStyle(fontSize: 20),
              ),
              onPressed: () => logic.logout(),
              child: Text('setting_logout'.tr), //选中的颜色字体颜色
            )),
      ],
    );
  }

  buildAccountInfoView(BuildContext context,SettingLogic logic, SettingState state){
    return Container(
        margin: const EdgeInsets.fromLTRB(10, 40, 10, 10),
        child:Table(
        children: buildTableRowList(context,logic,state),
        columnWidths: const {
          0: FlexColumnWidth(3),
          1: FlexColumnWidth(4),
        },
        border: const TableBorder(
          top: BorderSide(
            color: Colors.black,
            width: 1,
            style: BorderStyle.solid,
          ),
          left: BorderSide(
            color: Colors.black,
            width: 1,
            style: BorderStyle.solid,
          ),
          right: BorderSide(
            color: Colors.black,
            width: 1,
            style: BorderStyle.solid,
          ),
          bottom: BorderSide(
            color: Colors.black,
            width: 1,
            style: BorderStyle.solid,
          ),
          horizontalInside: BorderSide(
            color: Colors.black,
            width: 1,
            style: BorderStyle.solid,
          ),
          verticalInside:BorderSide(
            color: Colors.black,
            width: 1,
            style: BorderStyle.solid,
          ),
        ),
        defaultVerticalAlignment: TableCellVerticalAlignment.middle,
      )
    );
  }

  buildTableRowList(BuildContext context,SettingLogic logic, SettingState state) {
    return  [
    buildTableRow("setting_account".tr,
        state.user.value.psName!),
      buildTableRow("setting_phone".tr, state.user.value.psPhoneNum!),
      buildTableRow("setting_role".tr, state.user.value.getPsAuthName()),

      buildBottomTableRow("setting_valid".tr, state.user.value.psStartDate!, "-", state.user.value.psExDate!)
    ];
  }

  TableRow buildTableRow(String label, String value) {
    return
       TableRow(
        decoration: const BoxDecoration(
          color: Colors.lime,
        ),
        children: [
          TableCell(
            child:ConstrainedBox(
              constraints:  const BoxConstraints(minHeight: 36),
              child: Center(
                child: Text(label, style: const TextStyle(color:Colors.orange,fontSize: 20))))),
          TableCell(
            child:Container(
             color:  Colors.white,
             child:ConstrainedBox(
              constraints:  const BoxConstraints(minHeight: 36),
              child: Center(
                  child: Text(value, style: const TextStyle(fontSize: 20))))))
        ],
      )
    ;
  }

  TableRow buildBottomTableRow(String label, String lValue, String cValue, String rValue) {
    return
      TableRow(
        decoration: const BoxDecoration(
          color: Colors.lime,
        ),
        children: [
          TableCell(
              child:ConstrainedBox(
                  constraints:  const BoxConstraints(minHeight: 36),
                  child: Center(
                      child: Text(label, style: const TextStyle(color:Colors.orange,fontSize: 20))))),
          TableCell(
              child:Column(
                  children: [
                  Container(
                  color:  Colors.white,
                  child:ConstrainedBox(
                      constraints:  const BoxConstraints(minHeight: 36),
                      child: Center(
                          child: Text(lValue, style: const TextStyle(fontSize: 20))))),
                    createDivider(),
                    Container(
                      color:  Colors.white,
                      child:ConstrainedBox(
                          constraints:  const BoxConstraints(minHeight: 36),
                          child: Center(
                              child: Text(cValue, style: const TextStyle(fontSize: 20))))),
                    createDivider(),
                    Container(
                      color:  Colors.white,
                      child:ConstrainedBox(
                          constraints:  const BoxConstraints(minHeight: 36),
                          child: Center(
                              child: Text(rValue, style: const TextStyle(fontSize: 20))))),

              ]))
        ],
      )
    ;
  }

  Future<void> showSelectLanguageDialog(BuildContext context) async {
    bool isCz = Messages.isCzLocal;
    state.selectLanguage.value = isCz ? 0 : 1;
    return showDialog<void>(
      context: context,
      barrierDismissible: false, // 设置为false，点击背景不会关闭
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text("setting_language".tr),
          content: SingleChildScrollView(
            child: Obx(() => Column(
              children: [
                RadioListTile(
                  title: const Text('中文（繁）'),
                  value: 0,
                  groupValue: state.selectLanguage.value,
                  onChanged: (value) {
                    logic.selectLanguage(value!);
                  },
                ),
                RadioListTile(
                  title: const Text('English'),
                  value: 1,
                  groupValue: state.selectLanguage.value,
                  onChanged: (value) {
                    logic.selectLanguage(value!);
                  },
                ),
              ],
            ))
          ),
          actions: <Widget>[
            TextButton(
              child: Text("ok".tr),
              onPressed: () {
                Navigator.of(context).pop();
                logic.changeLanguage();
              },
            ),
            TextButton(
              child: Text("cancel".tr),
              onPressed: () {
                Navigator.of(context).pop();
                logic.cancelChangeLanguage();
              },
            ),
          ],
        );
      },
    );
  }
}
